﻿using System;
using System.Globalization;
using System.Linq;

namespace NTTDATA.BEENChina.ResultCertification.Split
{
    /// <summary>
    /// 在录入结果中
    /// 拆分出来 日期部分 入院期间部分  文字部分
    /// 
    /// 0913 add decimalFormat
    /// </summary>
    public class SplitEntryResult
    {
        #region 自定义字符串标记


        private readonly string[] dateFormat    = new []{ "gg","yy", "yyyy","MM", "dd", "tt","HH","mm","ss" };


        private readonly string[] strFormat = new[] {"tx"};
    
        private readonly string[] hpFormat  = new []{ "hp" };
        private readonly string[] telFormat = new []{ "tel", "tel_a", "tel_b", "tel_c" };

        //邮编\\县\\市\\町\\番号　Mansion名
        private readonly string[] addressFormat = new[] { "postalcode" ,"prefecture", "city", "town", "number & mansion" };

        //md : ７．１７．２１
        private readonly string[] decimalFormat =   new []{"md","d","ed"};
        //2013-11-12
        private readonly string[] nameFormat    =   new []{ "kana", "kannji" };

        private readonly char splitChar = '/';
        private readonly string  splitString = "/";

        #endregion

        #region 构造函数

        public SplitEntryResult(string format, string result)
        {
            this.Format = format.Split(new char[] { splitChar });
            this.EntryReuslt = result;
            this.ResultPart = result.Split(new char[] { splitChar });
        }

      /*  /// <summary>
        /// 文字列部分 需要用此构造函数
        /// 每次调用前都需要new
        /// </summary>
        /// <param name="mulitFormat"></param>
        /// <param name="currentRow"></param>
        /// <param name="result"></param>
        public SplitEntryResult(string mulitFormat, int currentRow, string result)
        {
            var formats = mulitFormat.Split(new string[] { @"\\" }, StringSplitOptions.None);
            
            if (currentRow < formats.Count())
            {
                
                this.Format = formats[currentRow].Split(new char[] { splitChar });
                this.EntryReuslt = result;
                this.ResultPart = result.Split(new char[] { splitChar });
            }
            
        }*/

        #endregion

        #region 公有属性

        /// <summary>
        /// 日期返回结果的相应格式
        /// example gg/yy/mm/dd
        /// </summary>
        public string  DateResultFormat
        {
            get;
            set;
        }
        
        #endregion

        #region 得到日期部分

        /// <summary>
        /// 日期部分的字符串
        /// 去掉 '
        /// </summary>
        /// <returns></returns>
        public string GetDate()
        {
            string date = GetSpecialString(dateFormat);

            //date = RemoveAccentSign(date);

            return date;
        }

        public DateTime? GetDateTime()
        {

            string entryChangeDateStr = string.Empty;

            try
            {
                //CultureInfo cci = new CultureInfo("ja-JP");
                //Calendar ccal = new JapaneseCalendar();
                //cci.DateTimeFormat.Calendar = ccal;
                //DateTime dt = DateTime.Parse("2006/09/26 13:34:20");
                //string strWareki = dt.ToString("ggyy/MM/dd/tt hh:mm:ss ", cci); 


                string entryDateStr = GetDate();
                if (entryDateStr.Contains("?"))
                    return null;

                
                // 月份 日期 位置上 补0 
                // 4/18/9/26 转换为  平成/18/09/26 
                entryChangeDateStr = Util.DateStrHelper.ChangeDateStr(entryDateStr, DateResultFormat);

         
                //结果中有空白项目 无法转换成日期类型 返回null
                var dateArr = entryChangeDateStr.Split(new char[] { splitChar });
                if (  dateArr.Contains(string.Empty))
                    return null;



                if (DateResultFormat.Contains("gg") || DateResultFormat.Contains("tt"))
                {
                    //entryChangeDateStr = "平成/21/8/13";
                    //DateResultFormat = "gg/yy/mm/dd";
                    //  平成18/09/26 转换为 2006/09/26 
                    CultureInfo ci = new CultureInfo("ja-JP");
                    Calendar cal = new JapaneseCalendar();
                    ci.DateTimeFormat.Calendar = cal;
                    return DateTime.ParseExact(entryChangeDateStr, DateResultFormat, ci);
                }
                else
                {
                    //yyyy-MM-dd

                    CultureInfo ci = new CultureInfo("en-US");
                    return DateTime.ParseExact(entryChangeDateStr, DateResultFormat, ci);
                }
            }
            catch (Exception ex)
            {
                //entryDateStr	"/1012/5/10"	string
                Log.Console.Error(string.Format("entryResult:{0} changeResult:{1} Format:{2}", EntryReuslt, entryChangeDateStr, DateResultFormat));

                Log.Console.Error("SplitEntryResult.GetDateTime", ex);
                // System.Console.WriteLine(ex.Message);
            }

            return null;
        }

        public string GetTt()
        {
            return GetSpecialString(new string[] { "tt" });
        }

        public string GetHour()
        {
            return GetSpecialString(new string[] { "HH" });
        }


        #endregion

        #region 得到通院治疗日

        public string GetHospital()
        {
            return GetSpecialString(hpFormat);
        }

        #endregion

        #region 得到电话部分

        public string GetTel()
        {
            return  GetSpecialString(telFormat); 
        }

        public string GetTelA()
        {
            return GetSpecialString(new string[] { "tel_a" });
        }

        #endregion

        #region 得到数值部分

        public string GetMd()
        {
            return GetSpecialString(new string[] { "md" });
        }

        //public string GetNumeric()
        //{
        //    return GetSpecialString(new string[] { "d","ed" });
        //}

        public string GetNumeric()
        {
            return GetSpecialString(decimalFormat);
        }

        #endregion

        #region 得到选择部分

        public string GetSelect()
        {
            return GetSpecialString(new [] { "sel" });
        }

        #endregion

        #region 得到姓名部分

        public string GetNameOfKana()
        {
            string result = GetSpecialString(new[] { "kana" });
            


            return result;
        }

        public string GetNameOfKannji()
        {
            string result = GetSpecialString(new[] { "kannji" });

            //汉字部分 不进行变换
            //result = Util.JaStringUtil.ChangeJaInputToHirakana(result);

            return result;
        }

        public string GetName()
        {
            string result = GetSpecialString(nameFormat);

           // result = Util.JaStringUtil.ChangeJaInputToHirakana(result);

            return result;
        }

        #endregion

        #region 得到文字部分

        public string GetText()
        {
            string result = GetSpecialString(new[] { "tx" });

            return result;
        }

        /// <summary>
        /// 病名
        /// </summary>
        /// <returns></returns>
        public string GetBingMing()
        {
            string result = GetSpecialString(new[] { "bm" });

            return result;
        }

        /// <summary>
        /// 手术名
        /// </summary>
        /// <returns></returns>
        public string GetShouShuMing()
        {
            string result = GetSpecialString(new[] { "ssm" });

            return result;
        }

        /// <summary>
        /// kj部分
        /// </summary>
        /// <returns></returns>
        public string GetKjCell()
        {
            string result = GetSpecialString(new[] { "kj" });

            return result;
        }

        /// <summary>
        /// 番号栏
        /// </summary>
        /// <returns></returns>
        public string GetNumberCell()
        {
            string result = GetSpecialString(new[] { "number" });

            return result;
        }

        /// <summary>
        /// 线量栏
        /// </summary>
        /// <returns></returns>
        public string GetXianLiangCell()
        {
            string result = GetSpecialString(new[] { "xl" });

            return result;
        }

        #endregion

        #region 得到住所部分

        public string GetAddress()
        {
            return GetSpecialString(addressFormat);
        }

        public string GetPrefecture()
        {
            return GetSpecialString(new[] { "prefecture" });
        }

        public string GetCity()
        {
            return GetSpecialString(new[] { "city" });
        }

        public string GetTown()
        {
            return GetSpecialString(new[] { "town" });
        }

        public string GetNumberMansion()
        {
            return GetSpecialString(new[] { "number&mansion" });
        }

        public string GetMansion()
        {
            string numberMansion = GetNumberMansion();
            return SplitAddressResult.GetMansionResult(numberMansion);
        }

        public string GetNumber()
        {
            string numberMansion = GetNumberMansion();
            return SplitAddressResult.GetNumberResult(numberMansion);
        }


        #endregion

        #region 私有函数部分

        private string EntryReuslt{get;set;}

        private string[] ResultPart
        {
            get;
            set;
        }

        private string[] Format
        {
            get;
            set;
        }

        /// <summary>
        /// remove '
        /// </summary>
        /// <param name="entryDateStr"></param>
        /// <returns></returns>
        private static string RemoveAccentSign(string entryDateStr)
        {
            //remove '
            // '13/4/25
            //  4/'25/1/1
            entryDateStr = entryDateStr.Replace("'", string.Empty);
            return entryDateStr;
        }

        private string GetSpecialString(string[] formatTags)
        {
            int begin = -1, end = -1;
         
            foreach (string str in formatTags)
            {
                begin = indexOf(str, this.Format);
                if (begin != -1)
                {
                    break;
                }
               
            }

            if (begin == -1)
            {
                return "";
            }

            for (int j = this.Format.Count() - 1; j >= begin; j--)
            {
                string str = this.Format[j];
                int idx = indexOf(str, formatTags );
                if (idx != -1)
                {
                    end = j;
                    break;
                }
            }

            if (begin >= ResultPart.Length )
            {
                return string.Empty;
            }

            if ( end >= ResultPart.Length )
            {
                end = ResultPart.Length - 1;
            }

            string[] dateResultArr = subArrayOf(begin, end, this.ResultPart);
            //返回日期的格式
            DateResultFormat = string.Join(splitString, subArrayOf(begin, end, this.Format));
            return string.Join(splitString, dateResultArr);

            //string result = null;
            //foreach (string s in arr)
            //{
            //    result = result ==null? s : result + splitChar + s;
            //}
            //返回日期
            //return result;
        }

        private int indexOf(string str, string[] array)
        {
            int idx =-1;
            for(int i=0;i<array.Count() ;i++)
            { 
                string s = array[i];
                if (str == s)
                {
                    idx = i;
                    break;
                }
            }
            return idx;
        }

        private string[] subArrayOf(int begin, int end, string[] array)
        {
            string[] subArr = new string[end - begin + 1];
            int j = 0;
            for (int i = begin; i <= end; i++,j++)
            {
                subArr[j] = array[i];
            }
            return subArr;
        }

        #endregion




    
    }   
}
